package com.mxx.v3.service.impl;

import com.mxx.v3.dao.mapper.TEchartMapper;
import com.mxx.v3.dao.mapper.TWorkpieceMouthTaskMapper;
import com.mxx.v3.dto.ChartData;
import com.mxx.v3.dto.ReportDto;
import com.mxx.v3.pojo.TEchart;
import com.mxx.v3.pojo.TEchartExample;
import com.mxx.v3.pojo.TWorkpieceMouthTask;
import com.mxx.v3.pojo.TWorkpieceMouthTaskExample;
import com.mxx.v3.service.ReportService;
import com.mxx.v3.web.query.ReportQuery;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program:report-from-server
 * @description:九宫格报表的实现
 * @author:AlTuring
 * @Date:2019/11/2-12:36
 **/
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private TWorkpieceMouthTaskMapper tWorkpieceMouthTaskMapper;
    @Autowired
    private TEchartMapper tEchartMapper;
    @Override
    public Object getChart1Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
        criteria.andTimeLike(time+"%");
        criteria.andEchartEqualTo("chart1");
        return returnPie(tEchartMapper.selectByExample(tEchartExample));
    }

//    public ReportDto getChart1Data(ReportQuery reportQuery) {
//        //查询的时间：年-月
//        String time = reportQuery.getMonth();
//        TWorkpieceMouthTaskExample tWorkpieceMouthTaskExample = new TWorkpieceMouthTaskExample();
//        TWorkpieceMouthTaskExample.Criteria criteria = tWorkpieceMouthTaskExample.createCriteria();
//        criteria.andMouthDateEqualTo(time);
//        List<TWorkpieceMouthTask> tWorkpieceMouthTaskList = tWorkpieceMouthTaskMapper.selectByExample(tWorkpieceMouthTaskExample);
//        //当月的完成数量；
//        int finishNum = 0;
//        //当月的未完成数量
//        int unfinishNum = 0;
//        //当月的产值
//        int outputValue = 0;
//        //累加，计算出当月所有计划的完成数量（PS：其实是计划数量，不是完后数量）
//        for (TWorkpieceMouthTask tWorkpieceMouthTask: tWorkpieceMouthTaskList) {
//            if (tWorkpieceMouthTask.getMouthAmount() != null){
//                finishNum += tWorkpieceMouthTask.getMouthAmount();
//            }
//        }
//        //数据库没有未完成数量，所以给一个暂时固定值
//        if (finishNum > 10000){
//            unfinishNum = finishNum - 10000;
//        }
//        //获取当前的月份
//        String month = time.substring(5,time.length());
//        //赋值
//        ReportDto reportDto = new ReportDto();
//        reportDto.setFinishNum(finishNum);
//        reportDto.setUnfinishNum(unfinishNum);
//        reportDto.setMonth(Integer.valueOf(month));
//        reportDto.setOutputValue(outputValue);
//        return reportDto;
//    }

//    public List<ReportDto> getChart2Data(ReportQuery reportQuery) {
//        //定义最后的存放结果的list
//        List<ReportDto> list = new ArrayList<>();
//        //当前月份 2019-10
//        String time = reportQuery.getMonth();
//        //获取年 2019
//        String year = time.substring(0,4);
//        //获取月份 10
//        Integer nowMonth = Integer.valueOf(time.substring(5,time.length()));
//        //计算每一个月的数据
//        TWorkpieceMouthTaskExample tWorkpieceMouthTaskExample = new TWorkpieceMouthTaskExample();
//        TWorkpieceMouthTaskExample.Criteria criteria = tWorkpieceMouthTaskExample.createCriteria();
//        while (nowMonth > 0){
//            //需要把月份处理一下，变成两位数，比如1月份= 01；
//            String queryMonth = String.format("%02d", nowMonth);
//            //拼接查询的时间
//            String queryTime = year+"-"+queryMonth;
//            criteria.andMouthDateEqualTo(queryTime);
//            List<TWorkpieceMouthTask> tWorkpieceMouthTaskList = tWorkpieceMouthTaskMapper.selectByExample(tWorkpieceMouthTaskExample);
//            //当月的完成数量；
//            int finishNum = 0;
//            //当月的未完成数量
//            int unfinishNum = 0;
//            //定义产值
//            int outputValue = 0;
//            //定义环比（当前月份的产值 / 上一个月的产值）
//            if (tWorkpieceMouthTaskList.size() > 0){
//                //累加，计算出当月所有计划的产值（PS：其实是计划数量，不是产值）
//                for (TWorkpieceMouthTask tWorkpieceMouthTask: tWorkpieceMouthTaskList) {
//                    if (tWorkpieceMouthTask.getMouthAmount() != null){
//                        outputValue += tWorkpieceMouthTask.getMouthAmount();
//                    }
//                }
//            }
//            ReportDto reportDto = new ReportDto();
//            reportDto.setFinishNum(finishNum);
//            reportDto.setUnfinishNum(unfinishNum);
//            reportDto.setOutputValue(outputValue);
//            reportDto.setMonth(nowMonth);
//            list.add(reportDto);
//
//            nowMonth--;
//        }
//        //计算环比
//        for (int i = 0; i < list.size(); i++) {
//            ReportDto reportDto = list.get(i);
//            //定义产值
//            if (i == (list.size() - 1)){
//                int outputValue = 0;
//                Integer lastYear = (Integer.valueOf(year) - 1);
//                //拼接查询的时间
//                String queryTime = lastYear+"-12";
//                criteria.andMouthDateEqualTo(queryTime);
//                List<TWorkpieceMouthTask> tWorkpieceMouthTaskList = tWorkpieceMouthTaskMapper.selectByExample(tWorkpieceMouthTaskExample);
//                if (tWorkpieceMouthTaskList.size() > 0){
//                    //累加，计算出当月所有计划的产值（PS：其实是计划数量，不是产值）
//                    for (TWorkpieceMouthTask tWorkpieceMouthTask: tWorkpieceMouthTaskList) {
//                        if (tWorkpieceMouthTask.getMouthAmount() != null){
//                            outputValue += tWorkpieceMouthTask.getMouthAmount();
//                        }
//                    }
//                }
//                BigDecimal bigDecimal;
//                if (outputValue == 0){
//                    bigDecimal = new BigDecimal("0");
//                }else {
//                    bigDecimal = new BigDecimal(reportDto.getOutputValue() / outputValue);
//                }
//                reportDto.setRingRatio(bigDecimal);
//            }else {
//                ReportDto lastReportDto = list.get(i + 1);
//                BigDecimal bigDecimal;
//                if (lastReportDto.getOutputValue() != 0){
//                    bigDecimal = new BigDecimal(reportDto.getOutputValue() / lastReportDto.getOutputValue());
//                }else {
//                    bigDecimal = new BigDecimal("0");
//                }
//                reportDto.setRingRatio(bigDecimal);
//            }
//        }
//        return list;
//    }
    @Override
    public Object getChart2Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
//        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
//        criteria.andTimeLike(time+"%");
        criteria.andEchartEqualTo("chart2");
        return returnBarAndLine(tEchartMapper.selectByExample(tEchartExample));
    }
    @Override
    public Object getChart3Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
        criteria.andTimeLike(time+"%");
        criteria.andEchartEqualTo("chart3");
        return returnPie(tEchartMapper.selectByExample(tEchartExample));
    }
    @Override
    public Object getChart4Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
        criteria.andEchartEqualTo("chart4");
        return returnBarAndLine(tEchartMapper.selectByExample(tEchartExample));
    }
    @Override
    public Object getChart5Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
        criteria.andTimeLike(time+"%");
        criteria.andEchartEqualTo("chart5");
        return returnBarAndLine(tEchartMapper.selectByExample(tEchartExample));
    }
    @Override
    public Object getChart6Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
        //String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
        criteria.andEchartEqualTo("chart6");
        return returnBarAndLine(tEchartMapper.selectByExample(tEchartExample));
    }
    @Override
    public Object getChart7Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
//        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
//        criteria.andTimeLike(time+"%");
        criteria.andEchartEqualTo("chart7");
        return returnBarAndLine(tEchartMapper.selectByExample(tEchartExample));
    }
    @Override
    public Object getChart8Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
        criteria.andTimeLike(time+"%");
        criteria.andEchartEqualTo("chart8");
        return returnPie(tEchartMapper.selectByExample(tEchartExample));
    }
    @Override
    public Object getChart9Data(ReportQuery reportQuery) {
        TEchartExample tEchartExample=new TEchartExample();
        TEchartExample.Criteria criteria=tEchartExample.createCriteria();
//        String time=reportQuery.getMonth().split("-")[0]+"-"+reportQuery.getMonth().split("-")[1];
//        criteria.andTimeLike(time+"%");
        criteria.andEchartEqualTo("chart9");
        return returnBarAndLine(tEchartMapper.selectByExample(tEchartExample));
    }
    //返回饼图的数据
    private List<ChartData.ChartBar> returnPie(List<TEchart> tEcharts){
        List<ChartData.ChartBar> list=new ArrayList<>();
        for(TEchart tEchart : tEcharts)
        {
            ChartData.ChartBar bar=new ChartData.ChartBar();
            bar.setName(String.valueOf(tEchart.getName()));
            bar.setValue(String.valueOf(tEchart.getValue()));
            list.add(bar);
        }
        return list;
    }
    //返回柱状图和折线图的数据
    private ChartData.ChartBarArray returnBarAndLine(List<TEchart> tEcharts){
        ChartData.ChartBarArray array=new ChartData.ChartBarArray();
        String[] name=new String[tEcharts.size()];
        String[] value=new String[tEcharts.size()];
        String[] chain_index= new String[tEcharts.size()];
        int i=0;
        for(TEchart tEchart : tEcharts)
        {
            name[i]=tEchart.getName();
            value[i]=tEchart.getValue();
            if(i>0&!StringUtils.isEmpty(name[i])&&!StringUtils.isEmpty(name[i-1]))
            {
                double temp=Double.valueOf(value[i])-Double.valueOf(value[i-1]);
                chain_index[i]=String.valueOf(temp/Double.valueOf(value[i-1])*100);
            }
            i++;
        }
        array.setName(name);
        array.setValue(value);
        array.setChain_index(chain_index);
        return array;
    }

}
